<?php

namespace pam\Mapper;

/**
 * Description of ResearchMapper
 *
 * @author Robert Bwana
 */
use Zend\Db\Adapter\AdapterInterface;
use pam\Model\Research;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Update;
use Zend\Db\Sql\Delete;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Stdlib\Hydrator\HydratorInterface;

class ResearchDataAccess {
    protected $dbAdapter;
    protected $hydrator;
    protected $research;
    protected $table;
    
    public function __construct(AdapterInterface $dbAdapter, HydratorInterface $hydrator, Research $research) {
        $this->dbAdapter = $dbAdapter;
        $this->hydrator = $hydrator;
        $this->research = $research;
        $this->table = 'pam_research';
    }

    public function delete($id) {
        $action = new Delete($this->table);
        $action->where(array(
            'id = ?' => $id
        ));
        
        $sql = new Sql($this->dbAdapter);
        $stmt = $sql->prepareStatementForSqlObject($action);
        $result = $stmt->execute();
        
        return (bool)$result->getAffectedRows();
    }

    /*
     * returns SocialResearch
     */

    public function find($id) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select($this->table);
        $select->where(array('id = ?' => $id));

        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();

        if ($result instanceof ResultInterface && $result->isQueryResult() && $result->getAffectedRows()) {
            return $this->hydrator->hydrate($result->current(), $this->research);
        } else {
            throw new \InvalidArgumentException("Research with given ID:{$id} not found.");
        }
    }

    /*
     * returns array of SocialResearch
     */

    public function findAll() {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select($this->table);

        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();

        if ($result instanceof ResultInterface && $result->isQueryResult()) {
            $resultSet = new HydratingResultSet($this->hydrator, $this->research);

            return $resultSet->initialize($result);
        } else {
            return array();
        }
    }

    public function save(Research $research) {
        $researchData = $this->hydrator->extract($research);
        unset($researchData['id']);

        if ($research->getId()) {
            $action = new Update($this->table);
            $action->set($researchData);
            $action->where(array('id = ?' => $research->getId()));
        } else {
            $action = new Insert($this->table);
            $action->values($researchData);
        }
        
        $sql = new Sql($this->dbAdapter);
        $stmt = $sql->prepareStatementForSqlObject($action);
        $result = $stmt->execute();
        
        if($result instanceof ResultInterface){
            if((bool)$newId = $result->getGeneratedValue()){
                $research->setId($newId);
            }
            
            return $research;
        }
    }
}
